﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using FbKPI.Facade.Validator;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using System.Web.Script.Services;
using System.Transactions;

namespace FBKPI.ProcessRequest
{
    /// <summary>
    /// Summary description for KPIMethodHandler
    /// </summary>
    public class KPIMethodHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string msg = this.Save();
            context.Response.Write(msg);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string Save()
        {
            string msg = "OK";
            int KpiId = Convert.ToInt32(HttpContext.Current.Request.QueryString["KpiId"]);
            string MethodDtlId = HttpContext.Current.Request.QueryString["MethodDtlId"].ToString();
            string Status = HttpContext.Current.Request.QueryString["Status"];
            int MethodID = Convert.ToInt16(HttpContext.Current.Request.QueryString["MethodID"]);
            string isOneSelection = HttpContext.Current.Request.QueryString["isOneSelection"];
            Employee CurrUser = new EmployeeFacade().GetEmployeeByID(Convert.ToInt32(HttpContext.Current.Request.QueryString["CurrUser"]));
            string CountMethodBy = HttpContext.Current.Request.QueryString["CountMethodBy"];
            string MaxDay = HttpContext.Current.Request.QueryString["MaxDay"];
            string UoMID = HttpContext.Current.Request.QueryString["UoMID"];
            string UoMDetailIDMax = HttpContext.Current.Request.QueryString["UoMDetailIDMax"];
            string UoMDetailIDMin = HttpContext.Current.Request.QueryString["UoMDetailIDMin"];
            string PercentageMax = HttpContext.Current.Request.QueryString["PercentageMax"];
            string PercentageMin = HttpContext.Current.Request.QueryString["PercentageMin"];

            string[] ArrMethodDtlId = MethodDtlId.Trim().Split('|');
            string[] ArrStatus = Status.Trim().Split('|');
            string[] ArrisOneSelection = isOneSelection.Trim().Split('|');
            string[] ArrMaxDay = MaxDay.Trim().Split('|');
            string[] ArrUoMID = UoMID.Trim().Split('|');
            string[] ArrUoMDetailIDMax = UoMDetailIDMax.Trim().Split('|');
            string[] ArrUoMDetailIDMin = UoMDetailIDMin.Trim().Split('|');
            string[] ArrPercentageMax = PercentageMax.Trim().Split('|');
            string[] ArrPercentageMin = PercentageMin.Trim().Split('|');


            List<KPIMethod> list = new List<KPIMethod>();
            for (int i = 0; i < ArrMethodDtlId.Length-1; i++)
            {
                if (ArrMethodDtlId[i].Trim() != "" && ArrMethodDtlId[i].Trim() != "undifined")
                {
                    KPIMethod obj = new KPIMethod();
                    obj.MethodID = MethodID;
                    obj.KpiId = KpiId;
                    obj.isOneSelection = Convert.ToInt32(ArrisOneSelection[i].ToString().Trim()) == 1 ? true : false;
                    obj.Status = Convert.ToInt32(ArrStatus[i].ToString().Trim()) == 1 ? true : false;
                    obj.MethodDtlId = Convert.ToInt32(ArrMethodDtlId[i].Trim());
                    obj.CreateDate = DateTime.Now;
                    obj.CreateBy = CurrUser.Email;
                    obj.CountMethodBy = CountMethodBy;
                    obj.MaxDay = Convert.ToInt16(ArrMaxDay[i]);
                    obj.UoMID = Convert.ToInt16(ArrUoMID[i]);
                    obj.UoMDetailIDMax = Convert.ToInt16(ArrUoMDetailIDMax[i]);
                    obj.UoMDetailIDMin = Convert.ToInt16(ArrUoMDetailIDMin[i]);
                    obj.PercentageMax = Convert.ToInt16(ArrPercentageMax[i]);
                    obj.PercentageMin = Convert.ToInt16(ArrPercentageMin[i]);
                    list.Add(obj);
                }

            }

            KPIMethodFacade methodFacade = new KPIMethodFacade();

            if (methodFacade.Save(list, KpiId))
            {
                msg = "Transaction Success";
            }
            else
            {
                msg = "Transaction Failed";
            }
           
            //////bool isActive = Convert.ToBoolean(HttpContext.Current.Request.QueryString["isActive"]);
            //////bool isheadoffice = Convert.ToBoolean(HttpContext.Current.Request.QueryString["isheadoffice"]);
            //////int RGMID = Convert.ToInt16(HttpContext.Current.Request.QueryString["RGM"]);
            //////Employee CurrUser = new EmployeeFacade().GetEmployeeByID(Convert.ToInt32(HttpContext.Current.Request.QueryString["CurrUser"]));

            //////LocationFacade lf = new LocationFacade();
            //////Location locations = lf.GetLocationByID(ID);
            //////LocationValidator dv = new LocationValidator();

            //////if (locations == null)
            //////{
            //////    locations = new Location();
            //////    locations.ID = ID;
            //////    locations.Description = Description;
            //////    locations.OrgID = ORGID;
            //////    locations.isActive = isActive;
            //////    locations.isHeadOffice = isheadoffice;
            //////    locations.RGM = new RGMFacade().GetRGMByID(RGMID);
            //////    locations.CreateBy = CurrUser.Email;
            //////    locations.BusinessGroupID = CurrUser.BusinessGroupID;
            //////    msg = dv.ValidateOnSave(locations);
            //////}
            //////else if (action != "delete")
            //////{
            //////    locations.Description = Description;
            //////    locations.OrgID = ORGID;
            //////    locations.isActive = isActive;
            //////    locations.isHeadOffice = isheadoffice;
            //////    locations.RGM = new RGMFacade().GetRGMByID(RGMID);
            //////    locations.LastModifiedBy = CurrUser.Email;
            //////    msg = dv.ValidateOnUpdate(locations);
            //////}
            //////else if (action == "delete")
            //////    locations.LastModifiedBy = CurrUser.Email;

            //////if (msg == "OK")
            //////{
            //////    using (TransactionScope trans = new TransactionScope(TransactionScopeOption.RequiresNew))
            //////    {
            //////        switch (action)
            //////        {
            //////            case "save":
            //////                msg = lf.Save(locations);
            //////                break;
            //////            case "delete":
            //////                msg = lf.BeforeDelete(locations);
            //////                if (msg == "0")
            //////                    msg = lf.Delete(locations);
            //////                else
            //////                    msg = "Data tidak bisa di hapus,dipakai pada tabel yang lain !";
            //////                break;
            //////        }
            //////        trans.Complete();
            //////        trans.Dispose();
            //////    }
            //////}

            return msg;
        }
    }
}